#pragma once
#include "../Comm/comm.hpp"
#include "../Comm/util.hpp"
#include "../Comm/log.hpp"
// #include "oj_model.hpp"
#include "oj_model_mysql.hpp"
#include "ctemplate/template.h"

namespace ns_view
{
    using namespace ns_util;
    using namespace ns_log;
    using namespace ns_model;
    class View
    {
    public:
        View() {}
        ~View() {}

    public:
        const std::string ctemplate_html_path = "./ctemplate_html/";
        void AllExpendHtml(const std::vector<Question> &questions, std::string *html)
        {
            // 1. 形成路径
            std::string path = ctemplate_html_path + "all_questions.html";
            // 2. 创建一个题目字典
            ctemplate::TemplateDictionary root("all_questions");
            for (const auto &q : questions)
            {
                // 3.创建一个子字典sub
                ctemplate::TemplateDictionary *sub = root.AddSectionDictionary("question_list");
                sub->SetValue("number", q._number);
                sub->SetValue("title", q._title);
                sub->SetValue("star", q._star);
            }

            // 4.创建需要被渲染的网页对象
            ctemplate::Template *tpl = ctemplate::Template::GetTemplate(path, ctemplate::DO_NOT_STRIP);

            // 5.将字典添加到对象中
            tpl->Expand(html, &root);
        }

        void OneExpendHtml(const Question &q, std::string *html)
        {
            // 1. 形成路径
            std::string path = ctemplate_html_path + "one_question.html";
            // 2. 创建一个题目字典
            ctemplate::TemplateDictionary root("one_question");
            root.SetValue("number", q._number);
            root.SetValue("title", q._title);
            root.SetValue("star", q._star);
            root.SetValue("desc", q._desc);
            root.SetValue("pre_code", q._header);

            // 3.创建需要被渲染的网页对象
            ctemplate::Template *tpl = ctemplate::Template::GetTemplate(path, ctemplate::DO_NOT_STRIP);

            // 4.将字典添加到对象中
            tpl->Expand(html, &root);
        }
    };
}